package org.fungene.controller;

import java.io.BufferedReader;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.fungene.services.david.DavidAnnot;
import org.fungene.services.david.DavidLauncher;
import org.fungene.services.david.DavidTool;
import org.fungene.services.david.DavidType;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class DavidController extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		BufferedReader bReader = request.getReader();
		String line = "";
		StringBuffer data = new StringBuffer();
		while ((line = bReader.readLine()) != null) {
			data.append(line);
		}

		DavidLauncher davidLauncher = new DavidLauncher(DavidType.GENE_SYMBOL, DavidAnnot.GO_BP, data.toString(), DavidTool.GENE_REPORT);
		JSONObject davidResult = davidLauncher.launch();

		PrintWriter pr = response.getWriter();
		pr.println(davidResult.toString());
		pr.close();
		return null;
	}

}
